//
//  HMWelcomeViewController.swift
//  WeiBo
//
//  Created by Feng on 2017/6/7.
//  Copyright © 2017年 feng. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class HMWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }

    //MARK: --界面布局
    func setupUI()
    {
        self.view.backgroundColor = UIColor(patternImage: UIImage(named: "ad_background")!)
        
        view.addSubview(userImageView)
        view.addSubview(welcomeLabel)
        
        //加载图片
        userImageView.sd_setImage(with: URL(string:(HMUserAccountViewModel.sharedUserAccount.userAccount?.avatar_large)!), placeholderImage: UIImage(named: "avatar_default_big"))
        
        //约束
        userImageView.snp.makeConstraints { (make) in
            make.top.equalTo(self.view).offset(180)
            make.centerX.equalTo(self.view)
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        
        welcomeLabel.snp.makeConstraints { (make) in
            make.top.equalTo(userImageView.snp.bottom).offset(10)
            make.centerX.equalTo(userImageView)
        }
    }
    
    //MARK: --设置弹簧动画, 在viewDidAppear() 中实现
    override func viewDidAppear(_ animated: Bool) {
        
        //一定要super
        super.viewDidAppear(animated)
        
        userImageView.snp.updateConstraints { (make) in
            
            make.top.equalTo(self.view).offset(100)
        }
        
        UIView.animate(withDuration: 1.0, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 5, options: [], animations: {
            
            self.view.layoutIfNeeded()
        }) { (_) in
            
            UIView.animate(withDuration: 1.0, animations: { 
                
                self.welcomeLabel.isHidden = false
            }, completion: { (_) in
                
                //切换界面
                NotificationCenter.default.post(name: NSNotification.Name(rawValue: SwitchRootViewControllerNotification), object: nil)
            })
        }
    }

    //设置图片
    private lazy var userImageView: UIImageView =
    {
        let imageView = UIImageView()
        
        imageView.image = UIImage(named: "avatar_default_big")
        //设置圆角
        imageView.layer.cornerRadius = 45
        imageView.layer.masksToBounds = true
        //设置边线
        imageView.layer.borderColor = UIColor.lightGray.cgColor
        imageView.layer.borderWidth = 1
        
        return imageView
    }()
    
    //设置label
    private lazy var welcomeLabel: UILabel =
    {
        let label = UILabel()
        
        label.text = "欢迎回来"
        label.textColor = UIColor.darkGray
        label.font = UIFont.systemFont(ofSize: 15)
        label.sizeToFit()
        
        //一开始设置透明度为0(或隐藏)
        label.isHidden = true
        
        return label
    }()
}
